<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input v-model="query.keyword" placeholder="关键字"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="querySerch">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="exportExcel">导出</el-button>
        </el-form-item>
        <el-form-item label="" prop="resource">
          <el-col :span="12">
            <el-upload
                ref="upload"
                class="upload-demo"
                action="http://localhost:8080/logininfo/import/excel"
                :on-success="onSuccessFile">
              <el-button  type="primary">导入</el-button>
            </el-upload>
          </el-col>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="pageData.rows" highlight-current-row v-loading="listLoading" @selection-change="selsChange"
              style="width: 100%;">

<!--      <el-table-column type="expand">-->
<!--        <template slot-scope="props">-->
<!--          <el-table :data="props.row.auditLogs" style="width: calc(100% - 47px)" class="two-list">-->
<!--            <el-table-column type="selection" width="55">-->
<!--            </el-table-column>-->
<!--            <el-table-column type="index" width="60">-->
<!--            </el-table-column>-->
<!--            <el-table-column prop="state" label="状态">-->
<!--              <template slot-scope="props">-->
<!--							  <span v-if="props.row.state ==1">-->
<!--								<span style="color:#00B46D">通过</span>-->
<!--							  </span>-->
<!--                <span v-else>-->
<!--								<span style="color:#D75C89">驳回</span>-->
<!--                 			 </span>-->
<!--              </template>-->
<!--            </el-table-column>-->
<!--            <el-table-column prop="note" label="备注"></el-table-column>-->
<!--            <el-table-column prop="auditId" label="审核人员"></el-table-column>-->
<!--            <el-table-column prop="auditTime" label="审核时间"></el-table-column>-->
<!--          </el-table>-->
<!--        </template>-->
<!--      </el-table-column>-->

      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="username" label="用户名" width="120" sortable>
      </el-table-column>
      <el-table-column prop="phone" label="联系电话" width="150" sortable>
      </el-table-column>
      <el-table-column prop="email" label="邮箱" width="280" sortable>
      </el-table-column>

      <el-table-column prop="salt" label="盐值" min-width="30" sortable>
      </el-table-column>

      <el-table-column prop="password" label="密码" min-width="30" err sortable>
      </el-table-column>

      <el-table-column label="操作" width="250">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
<!--          <el-button size="small" :disabled="scope.row.state !== 0" @click="handleAudit(scope.$index, scope.row)">审核</el-button>-->
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination layout="prev, pager, next"
                     @current-change="handleCurrentChange"
                     :current-page="query.currentPage"
                     :page-size="query.pageSize"
                     :total="pageData.totals"
                     style="float:right;">
      </el-pagination>
    </el-col>

    <!--新增修改界面-->
    <el-dialog :title="title" :visible.sync="logininfoVisible" :close-on-click-modal="false">
      <el-form :model="logininfo" label-width="80px" :rules="logininfoFormRules" ref="logininfoForm">
        <el-form-item label="用户名" prop="name">
          <el-input v-model="logininfo.username" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="联系电话" prop="tel">
          <el-input v-model="logininfo.phone" auto-complete="off"></el-input>
        </el-form-item>
<!--        <el-form-item label="状态">-->
<!--          <el-radio-group v-model="logininfo.state">-->
<!--            <el-radio class="radio" :label="1">审核通过</el-radio>-->
<!--            <el-radio class="radio" :label="0">待审核</el-radio>-->
<!--            <el-radio class="radio" :label="-1">驳回</el-radio>-->
<!--            <el-radio class="radio" :label="2">待激活</el-radio>-->
<!--          </el-radio-group>-->
<!--        </el-form-item>-->
        <el-form-item label="邮箱" prop="address">
          <el-input v-model="logininfo.email" auto-complete="off"></el-input>
        </el-form-item>
        <!--        <el-form-item label="图标" prop="logo">-->
        <!--          <el-input v-model="logininfo.logo" auto-complete="off"></el-input>-->
        <!--        </el-form-item>-->
<!--        <el-form-item prop="logo" label="店铺Logo">-->
<!--          <el-upload-->
<!--              class="upload-demo"-->
<!--              action="http://localhost:8080/fastdfs/"-->
<!--              :on-preview="handlePreview"-->
<!--              :on-remove="handleRemove"-->
<!--              :on-success="handleSuccess"-->
<!--              :file-list="fileList"-->
<!--              list-type="picture">-->
<!--            <el-button size="small" type="primary">点击上传</el-button>-->
<!--            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>-->
<!--          </el-upload>-->
<!--        </el-form-item>-->

<!--        <el-form-item label="店长">-->
<!--          <el-select v-model="logininfo.adminId" placeholder="请选择">-->
<!--            <el-option-->
<!--                v-for="employee in employees"-->
<!--                :label="employee.username"-->
<!--                :value="employee.id">-->
<!--              &lt;!&ndash; 存在下面两个的时候，下拉框展示，优先展示下面的&ndash;&gt;-->
<!--              <span style="float: left">{{ employee.username }}</span>-->
<!--              <span style="float: right; color: #8492a6; font-size: 13px">{{ employee.phone }}</span>-->
<!--            </el-option>-->
<!--          </el-select>-->
<!--        </el-form-item>-->
        <el-form-item label="盐值" prop="address">
          <el-input v-model="logininfo.salt" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="address">
          <el-input v-model="logininfo.password" auto-complete="off"></el-input>
        </el-form-item>


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="deleteBlurWarn">取消</el-button>
        <el-button type="primary" @click.native="save" :loading="logininfoLoading">提交</el-button>
      </div>
    </el-dialog>

    <!--店铺审核界面-->
    <el-dialog title="审核店铺" :visible.sync="logininfoAuditVisible" :close-on-click-modal="false">
      <el-form :model="logininfoAuditLog" label-width="80px" ref="logininfoAuditLogForm">
        <el-form-item label="备注" prop="note">
          <el-input type="textarea" v-model="logininfoAuditLog.note"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button>取消</el-button>
        <el-button type="primary" @click.native="auditPass">通过</el-button>
        <el-button type="primary" @click.native="auditReject">驳回</el-button>
      </div>
    </el-dialog>

  </section>
</template>

<script>

export default {
  data() {
    return {
      src:'',
      title: '',
      employees: [],
      filters: {
        name: ''
      },
      pageData: {
        totals: 0,
        rows: [],
        auditLogs: []
      },
      query: {
        currentPage: 1,
        pageSize: 5,
        keyword: ""
      },
      listLoading: false,
      sels: [],//列表选中列

      logininfoVisible: false,//编辑界面是否显示
      logininfoAuditVisible: false,//编辑界面是否显示
      logininfoLoading: false,
      logininfoFormRules: {
        username: [
          {required: true, message: '请输入用户名名称', trigger: 'blur'}
        ],
        phone: [
          {required: true, message: '请输入手机号', trigger: 'blur'}
        ],
        email: [
          {required: true, message: '请输入邮箱', trigger: 'blur'}
        ],
        salt: [
          {required: true, message: '请输入盐值', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'}
        ],

      },
      // 审核日志页面数据
      // auditLogs:{
      //   state: null,
      //   note: '',
      //   username: '',
      //   auditTime: ''
      // },
      //编辑界面数据
      logininfoAuditLog: {
        logininfoId: null,
        note: '',
        type: null // -1驳回 1 通过
      },
      //编辑界面数据
      logininfo: {
        id: null,
        username: '',
        phone: '',
        email: '',
        salt: '',
        password: '',
      }

    }
  },
  methods: {
    //文件上传成功回调
    handleSuccess(response, file, fileList){
      // console.log(response) 里面的resultObj就是我们图片的地址
      // console.log(file)
      // console.log(fileList)
      this.logininfo.logo = response.resultObj;
    },

    //文件删除
    handleRemove(file, fileList) {
      var filePath =file.response.resultObj;
      this.$http.delete("/fastdfs/?filePath="+filePath)
          .then(res=>{
            if(res.data.success){
              this.$message({
                message: '删除成功!',
                type: 'success'
              });
            }else{
              this.$message({
                message: '删除失败!',
                type: 'error'
              });
              this.getpagelist();
            }
          })
    },
    //图片预览
    handlePreview(file) {
      console.log(file);
    },

    // 导入成功之后消除文件列表，并给出提示
    onSuccessFile(){
      this.$refs.upload.clearFiles();
      this.$message({
        message: '导入成功',
        type: 'success'
      });
    },
    exportExcel(){
      location.href = "http://localhost:8080/logininfo/export/excel"
      //  this.$http.post("/logininfo/export/excel")

    },

    // 审核驳回
    auditReject() {
      console.log(this.logininfoAuditLog)
      this.logininfoAuditLog.type = -1;
      this.$http.post("/logininfo/audit", this.logininfoAuditLog)
          .then(result => {
            result = result.data;
            if (result.success) {
              this.$message({
                message: '驳回Ok',
                type: 'success'
              });
            } else {
              this.$message({
                message: '驳回失败',
                type: 'error'
              });
            }
            this.logininfoAuditVisible = false;
            this.getpagelist();
          })
    },
    // 审核通过
    auditPass() {
      this.logininfoAuditLog.type = 1;
      this.$http.post("/logininfo/audit", this.logininfoAuditLog)
          .then(result => {
            result = result.data;
            if (result.success) {
              this.$message({
                message: '操作成功',
                type: 'success'
              });
            } else {
              this.$message({
                message: '审核失败',
                type: 'error'
              });
            }
            this.logininfoAuditVisible = false;
            this.getpagelist();
          })
    },
    //清除讲校验提示
    deleteBlurWarn() {
      this.logininfoVisible = false
    },
    //性别显示转换
    formatSex: function (row, column) {
      return row.sex == 1 ? '男' : row.sex == 0 ? '女' : '未知';
    },
    handleCurrentChange(currentPage) {
      this.query.currentPage = currentPage
      this.getpagelist();
    },
    //删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        //NProgress.start();
        let para = {id: row.id};
        this.$http.delete("/logininfo/" + para.id).then((res) => {
          this.listLoading = false;
          this.getpagelist();
        }).catch(
            alert(res.data.message)
        );
      }).catch(() => {

      });
      this.getpagelist();
    },
    //显示编辑界面
    handleEdit: function (index, row) {
      this.title = "编辑"
      this.logininfoVisible = true;
      this.logininfo = Object.assign({}, row);
      //this.getEmployees()
    },
    //显示审核界面
    handleAudit: function (index, row) {
      this.logininfoAuditVisible = true;
      console.log(row)
      this.logininfoAuditLog.logininfoId = row.id;
    },
    //显示新增界面
    handleAdd: function () {
      this.title = "新增"
      this.logininfoVisible = true;
      // this.handleRemove()
      //this.getEmployees()
      this.logininfo = {
        id: null,
        username: '',
        phone: '',
        email: '',
        salt: '',
        password: '',
      };
    },
    //保存
    save: function () {
      this.$refs.logininfoForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.logininfoLoading = true;
            //NProgress.start();
            let para = Object.assign({}, this.logininfo);
            console.log(para)

            this.$http.put("/logininfo", para)
                .then((res) => {
                  this.logininfoLoading = false;
                  //NProgress.done();
                  if (res.data.success) {
                    this.$message({
                      message: '保存成功',
                      type: 'success'
                    });
                    this.$refs['logininfoForm'].resetFields();
                    this.logininfoVisible = false;
                    this.handleRemove();
                    this.getpagelist();
                    // this.handleRemove();
                  } else {
                    this.$message({
                      message: res.data.msg,
                      type: 'error'

                    });
                  }
                  this.getpagelist();

                });
          });
        }
      });
    },
    selsChange: function (sels) {
      this.sels = sels;
    },
    //批量删除
    batchRemove: function () {
      var ids = this.sels.map(item => item.id);
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        //NProgress.start();
        this.$http.patch("/logininfo", ids)
            .then((res) => {
              this.listLoading = false;
              //NProgress.done();
              if (res.data.success) {
                this.$message({
                  message: '删除成功',
                  type: 'success',
                });
              } else {
                this.$message({
                  message: res.data.message,
                  type: 'error'
                });
              }

              this.querySerch();
            });
      }).catch(() => {

      });
    },
    //高级查询
    querySerch() {
      this.query.currentPage = 1
      this.getpagelist()
    },
    //获取所有用户
    getEmployees() {
      this.$http.get("/emp").then(res => {
        if (res) {
          this.employees = res.data
        }
      })
    },

    //获取分页列表店铺
    getpagelist() {
      this.$http.post("/logininfo/list", this.query).then(res => {
        this.pageData = res.data
       // console.log(scope.row.logo)
      })
    }
  },

  // // 获取审核日志列表
  // getauditLogs(){
  //   let para = {id: row.id};
  //   this.$http.post("/logininfoAuditLog/findLog",para.id)
  //       .then(res => {
  //           this.auditLogs = res.data
  //       })
  // },

  mounted() {
    this.getpagelist();
    //this.getauditLogs();
  }
}


</script>

<style scoped>

</style>